package com.lee.common.widget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lee.common.R

/**
 * desripbe EmptyView
 */
class EmptyView(val context: Context) {

    private lateinit var emptyView: View
    private lateinit var errView: View
    private lateinit var loadingView: View
    private lateinit var inflater: LayoutInflater


    var adapter: BaseQuickAdapter<*, *>? = null

    /**
     * 数据返回空显示的View
     */
    fun getEmptyView(): View {
        if (!::emptyView.isInitialized)
            emptyView = getInflater().inflate(R.layout.layout_empty, null, false)
        return emptyView
    }

    fun values(states: Int) {
        if (adapter == null) return
        if (states == 1) {
            adapter?.setEmptyView(getEmptyView())
        }
        if (states == 2) {
            adapter?.setEmptyView(getErrView())
        }
    }

    /**
     * 正在获取数据时显示View
     */
    fun getLoadingView(): View {
        if (!::loadingView.isInitialized)
            loadingView = getInflater().inflate(R.layout.layout_loading_view, null, false)
        return loadingView
    }

    /**
     * 获取数据错误时显示View
     */
    fun getErrView(): View {
        if (!::errView.isInitialized)
            errView = getInflater().inflate(R.layout.layout_error, null, false)
        return errView
    }

    private fun getInflater(): LayoutInflater {
        if (!::inflater.isInitialized)
            inflater = LayoutInflater.from(context)
        return inflater
    }

}
